/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Figures 4a, 4b, 5a and 5b
=========================================================================*/

use "${data}/data_RD", clear
keep if icfes_per==20142 | icfes_per==20132 | icfes_per==20122
replace icfes_per=20132 if icfes_per==20122

glo cond_saber11 "eligible_sisben==1"
glo cond_sisben	"eligible_saber11==1"
glo no_cond_saber11	"eligible_sisben==0"
glo no_cond_sisben	"eligible_saber11==0"
glo lim_saber11	40
glo lim_sisben 20

preserve
	keep if icfes_per==20142
	cap drop mean_include*
	cap drop cut_saber11 
	cap drop cut_sisben
	gegen cut_saber11=cut(running_saber11), at(-100(2)100)
	gegen mean_include_saber11 = tag(cut_saber11) if eligible_sisben==1
	gegen cut_sisben=cut(running_sisben), at(-40(1)40)
	gegen mean_include_sisben = tag(cut_sisben) if eligible_saber11==1

	rdrobust graduation_exam_pro_7 running_saber11 if ${cond_saber11}, masspoints(off) 
	local bw=e(h_l)
	cap drop kw
	gen kw = ((((`bw')-abs(running_saber11))/(`bw'))*(abs(running_saber11) < (`bw')))
	cap drop bin
	gegen bin =  mean(graduation_exam_pro_7) if ${cond_saber11}, by(cut_saber11)
	
	#delimit ;
	twoway	(lfit graduation_exam_pro_7 running_saber11 [pw=kw] if abs(running_saber11)<`bw' & ${cond_saber11} & running_saber11 < 0, sort lcolor(black)) 
			(lfit graduation_exam_pro_7 running_saber11 [pw=kw] if abs(running_saber11)<`bw' & ${cond_saber11} & running_saber11 >= 0, sort lcolor(black)) 
			(scatter bin cut_saber11 if mean_include_saber11==1 & abs(running_saber11)<${lim_saber11}, mc(black) ms(O) msize(small)), 
			xline(0, lcolor(red) lpattern(dash)) xsize(8) xlab(-40(20)40) ylab(0(0.2)1) xtitle(Distance to SABER 11 cutoff) caption(Sample restricted to low-SES students., size(small)) legend(off) graphregion(color(white)) ytitle("")
	;
	#delimit cr
	
	graph export "${outcomes}/Fig4A.pdf", replace 

	rdrobust score_saberpro_5 running_saber11 if ${cond_saber11}, masspoints(off) 
	local bw=e(h_l)
	cap drop kw
	gen kw = ((((`bw')-abs(running_saber11))/(`bw'))*(abs(running_saber11) < (`bw')))
	cap drop bin
	gegen bin =  mean(score_saberpro_5) if ${cond_saber11}, by(cut_saber11)
	
	#delimit ;
	twoway	(lfit score_saberpro_5 running_saber11 [pw=kw] if abs(running_saber11)<`bw' & ${cond_saber11} & running_saber11 < 0, sort lcolor(black)) 
			(lfit score_saberpro_5 running_saber11 [pw=kw] if abs(running_saber11)<`bw' & ${cond_saber11} & running_saber11 >= 0, sort lcolor(black)) 
			(scatter bin cut_saber11 if mean_include_saber11==1 & abs(running_saber11)<${lim_saber11}, mc(black) ms(O) msize(small)), 
			xline(0, lcolor(red) lpattern(dash)) xsize(8) xlab(-40(20)40) ysc(r(-0.25 1)) ylab(-0.25(0.25)1) xtitle(Distance to SABER 11 cutoff) caption(Sample restricted to low-SES students., size(small)) legend(off) graphregion(color(white)) ytitle("")
	;
	#delimit cr
	
	graph export "${outcomes}/Fig5A.pdf", replace 
restore

cap drop mean_include*
cap drop cut_saber11 
cap drop cut_sisben
gegen cut_saber11=cut(running_saber11), at(-40(2)40)
gegen mean_include_saber11 = tag(cut_saber11 icfes_per) if eligible_sisben==1
gegen mean_exclude_saber11 = tag(cut_saber11 icfes_per) if eligible_sisben==0

gegen cut_sisben=cut(running_sisben), at(-20(1)20)
gegen mean_include_sisben = tag(cut_sisben icfes_per) if eligible_saber11==1
gegen mean_exclude_sisben = tag(cut_sisben icfes_per) if eligible_saber11==0

cap drop bin1 bin2 bin3 bin4
gegen bin1 =  mean(graduation_exam_pro_7) if ${cond_saber11} & icfes_per==20142 , by(cut_saber11) // eligible 2014
gegen bin2 =  mean(graduation_exam_pro_7) if ${cond_saber11} & icfes_per==20132, by(cut_saber11) // eligible 2013-2012
gegen bin3 =  mean(graduation_exam_pro_7) if ${no_cond_saber11} & icfes_per==20142, by(cut_saber11) // non eligible 2014 
gegen bin4 =  mean(graduation_exam_pro_7) if ${no_cond_saber11} & icfes_per==20132, by(cut_saber11) // non eligible 2013-2012

#delimit ;
twoway 	(lfit graduation_exam_pro_7 running_saber11 if abs(running_saber11)<${lim_saber11} & ${cond_saber11} & running_saber11 < 0 & icfes_per==20132, sort lcolor(gs12))
		(lfit graduation_exam_pro_7 running_saber11 if abs(running_saber11)<${lim_saber11} & ${cond_saber11} & running_saber11 >= 0 & icfes_per==20132, sort lcolor(gs12))
		(scatter bin2 cut_saber11 if mean_include_saber11==1 & icfes_per==20132, mc(gs12) ms(Oh) msize(small))
		(lfit graduation_exam_pro_7 running_saber11 if abs(running_saber11)<${lim_saber11} & ${no_cond_saber11} & running_saber11 < 0 & icfes_per==20132, sort lcolor(gs8) lpattern(dash))
		(lfit graduation_exam_pro_7 running_saber11 if abs(running_saber11)<${lim_saber11} & ${no_cond_saber11} & running_saber11 >= 0 & icfes_per==20132, sort lcolor(gs8) lpattern(dash))
		(scatter bin4 cut_saber11 if mean_exclude_saber11==1 & icfes_per==20132, mc(gs8) ms(Dh) msize(small))
		(lfit graduation_exam_pro_7 running_saber11 if abs(running_saber11)<${lim_saber11} & ${no_cond_saber11} & running_saber11 < 0 & icfes_per==20142, sort lcolor(gs4) lpattern(dash))
		(lfit graduation_exam_pro_7 running_saber11 if abs(running_saber11)<${lim_saber11} & ${no_cond_saber11} & running_saber11 >= 0 & icfes_per==20142, sort lcolor(gs4) lpattern(dash))
		(scatter bin3 cut_saber11 if mean_exclude_saber11==1 & icfes_per==20142, mc(gs4) ms(S) msize(small))
		(lfit graduation_exam_pro_7 running_saber11 if abs(running_saber11)<${lim_saber11} & ${cond_saber11} & running_saber11 < 0 & icfes_per==20142, sort lcolor(gs0)) 
		(lfit graduation_exam_pro_7 running_saber11 if abs(running_saber11)<${lim_saber11} & ${cond_saber11} & running_saber11 >= 0 & icfes_per==20142, sort lcolor(gs0))
		(scatter bin1 cut_saber11 if mean_include_saber11==1 & icfes_per==20142, mc(gs0) ms(T) msize(small)),
		legend(order(3 "Low SES pre" 6 "High SES pre" 12 "Low SES post" 9 "High SES post") position(10) ring(0) col(1)) 
		xline(0, lcolor(red) lpattern(dash)) xsize(8) xlab(-40(20)40) ylab(0(0.2)1) xtitle(Distance to SABER 11 cutoff)  graphregion(color(white)) ytitle("")
;
#delimit cr

graph export "${outcomes}/Fig4B.pdf", replace 


cap drop bin1 bin2 bin3 bin4
gegen bin1 =  mean(score_saberpro_5) if ${cond_saber11} & icfes_per==20142, by(cut_saber11) // eligible 2014
gegen bin2 =  mean(score_saberpro_5) if ${cond_saber11} & icfes_per==20132, by(cut_saber11) // eligible 2013-2012
gegen bin3 =  mean(score_saberpro_5) if ${no_cond_saber11} & icfes_per==20142, by(cut_saber11) // non eligible 2014 
gegen bin4 =  mean(score_saberpro_5) if ${no_cond_saber11} & icfes_per==20132, by(cut_saber11) // non eligible 2013-2012

#delimit ;
twoway 	(lfit score_saberpro_5 running_saber11 if abs(running_saber11)<${lim_saber11} & ${cond_saber11} & running_saber11 < 0 & icfes_per==20132, sort lcolor(gs12))
		(lfit score_saberpro_5 running_saber11 if abs(running_saber11)<${lim_saber11} & ${cond_saber11} & running_saber11 >= 0 & icfes_per==20132, sort lcolor(gs12))
		(scatter bin2 cut_saber11 if mean_include_saber11==1 & icfes_per==20132, mc(gs12) ms(Oh) msize(small))
		(lfit score_saberpro_5 running_saber11 if abs(running_saber11)<${lim_saber11} & ${no_cond_saber11} & running_saber11 < 0 & icfes_per==20132, sort lcolor(gs8) lpattern(dash))
		(lfit score_saberpro_5 running_saber11 if abs(running_saber11)<${lim_saber11} & ${no_cond_saber11} & running_saber11 >= 0 & icfes_per==20132, sort lcolor(gs8) lpattern(dash))
		(scatter bin4 cut_saber11 if mean_exclude_saber11==1 & icfes_per==20132, mc(gs8) ms(Dh) msize(small))
		(lfit score_saberpro_5 running_saber11 if abs(running_saber11)<${lim_saber11} & ${no_cond_saber11} & running_saber11 < 0 & icfes_per==20142, sort lcolor(gs4) lpattern(dash))
		(lfit score_saberpro_5 running_saber11 if abs(running_saber11)<${lim_saber11} & ${no_cond_saber11} & running_saber11 >= 0 & icfes_per==20142, sort lcolor(gs4) lpattern(dash))
		(scatter bin3 cut_saber11 if mean_exclude_saber11==1 & icfes_per==20142, mc(gs4) ms(S) msize(small))
		(lfit score_saberpro_5 running_saber11 if abs(running_saber11)<${lim_saber11} & ${cond_saber11} & running_saber11 < 0 & icfes_per==20142, sort lcolor(gs0)) 
		(lfit score_saberpro_5 running_saber11 if abs(running_saber11)<${lim_saber11} & ${cond_saber11} & running_saber11 >= 0 & icfes_per==20142, sort lcolor(gs0))
		(scatter bin1 cut_saber11 if mean_include_saber11==1 & icfes_per==20142, mc(gs0) ms(T) msize(small)),
		legend(order(3 "Low SES pre" 6 "High SES pre" 12 "Low SES post" 9 "High SES post") position(10) ring(0) col(1)) 
		xline(0, lcolor(red) lpattern(dash)) xsize(8) xlab(-40(20)40) ysc(r(-0.25 1)) ylab(-0.25(0.25)1) xtitle(Distance to SABER 11 cutoff) graphregion(color(white)) ytitle("")
;
#delimit cr

graph export "${outcomes}/Fig5B.pdf", replace 
